/**
 * 环境类型
 */
export type Environment = 'development' | 'production' | 'test';

/**
 * 应用程序配置类
 */
export class AppConfig {
  public readonly env: Environment;
  public readonly port: number;
  public readonly host: string;
  public readonly nodeEnv: string;

  constructor() {
    // 从环境变量获取配置，提供默认值
    this.env = (process.env.NODE_ENV as Environment) || 'development';
    this.port = parseInt(process.env.PORT || '5252', 10);
    this.host = process.env.HOST || 'localhost';
    this.nodeEnv = process.env.NODE_ENV || 'development';

    // 验证配置
    this.validate();
  }

  /**
   * 验证配置是否有效
   */
  private validate(): void {
    if (isNaN(this.port) || this.port < 1 || this.port > 65535) {
      throw new Error(`Invalid port number: ${this.port}`);
    }

    if (!['development', 'production', 'test'].includes(this.env)) {
      throw new Error(`Invalid environment: ${this.env}`);
    }
  }

  /**
   * 检查是否为开发环境
   */
  get isDevelopment(): boolean {
    return this.env === 'development';
  }

  /**
   * 检查是否为生产环境
   */
  get isProduction(): boolean {
    return this.env === 'production';
  }

  /**
   * 检查是否为测试环境
   */
  get isTest(): boolean {
    return this.env === 'test';
  }

  /**
   * 获取完整的服务器地址
   */
  get serverUrl(): string {
    return `http://${this.host}:${this.port}`;
  }

  /**
   * 输出配置信息（用于调试）
   */
  toString(): string {
    return JSON.stringify(
      {
        env: this.env,
        port: this.port,
        host: this.host,
        serverUrl: this.serverUrl,
      },
      null,
      2
    );
  }
}
